import nProgress from 'nprogress'
import router from './router'
import store from './store'
import 'nprogress/nprogress.css' // progress bar style

const whiteList = ['/login' , '/404']
router.beforeEach(async (to,form,next) => {
  nProgress.start()
  if(store.getters.token) {
    if(to.path ==='/login') {
      next('/')
      nProgress.done()
    }else {
      // 登陆了不去登录页，是否有用户资料
      if (!store.getters.name) {
         await store.dispatch('user/getUserInfoAction')
      } 
        // 放行
        next()
    }
  }else {
    if(whiteList.includes(to.path)) {
      next()
    }else {
      next('/login')
      nProgress.done()
    }
  }
})

router.afterEach((to,form,next) => {
  nProgress.done()
}) 



